//
//  TBCReadingListViewController.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCReadingListViewController.h"
#import "TBCReadingListView.h"
#import "TBCReadingList.h"
#import "TBCReadingWebViewController.h"

@interface TBCReadingListViewController () <TBCReadingListView>

@property (strong, nonatomic) TBCReadingListView *viewList;
@property (assign, nonatomic) NSInteger dataIndex;

@end

@implementation TBCReadingListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataIndex = 10;
    
    [self initListViews];
    [self initListData];
    
    UIImage *image = [UIImage imageNamed:@"fanhui.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction:)];
    
}

//自定义左侧按钮
-(void)leftBarAction:(UIBarButtonItem *)barItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initListViews {
    
    self.viewList = [[TBCReadingListView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.viewList.delegate = self;
    [self.view addSubview:self.viewList];
    
}

- (void)initListData {
    
    NSString *url = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *parameters = @{@"auth":@"C807FCuwm36lDC8l3HMTslDJxQMKpNLeNoj1vCGMzcWP22NZLTRhY4fI",@"client":@"1",@"deviceid":@"63900960-1ED5-47D0-B561-86C09F41ED08",@"limit":@"10",@"sort":@"addtime",@"start":[NSString stringWithFormat:@"%ld", self.dataIndex],@"typeid":[NSString stringWithFormat:@"%ld", self.typeID],@"version":@"3.0.6"};
    NSString *cookie = @"PHPSESSID=58ts41v1nohdn89damknm0vdt5";
    
    [self getDataFromAFN:url Parameters:parameters Cookie:cookie];
    
}

#pragma mark -- 请求数据
- (void)getDataFromAFN:(NSString *)url Parameters:(NSDictionary *)parameters Cookie:(NSString *)cookie {
    
#pragma mark -- 判断是否有网络
    
    NSInteger result = [TBCAppTools getInternetReachability:url];
    
    if (result >= InternetReachableWithWWAN) {
        
        [TBCAppTools postLocalDataWithURL:url Parameters:parameters Cookie:cookie TBCToolsBlock:^(id responseObject) {
            
            [[TBCReadingList shareReadingList] getAllArray:responseObject];
            
        }];
        
    }
    
}

- (void)pushToController:(NSString *)contentID {
    
    TBCReadingWebViewController *viewController = [[TBCReadingWebViewController alloc] init];
    viewController.contentID = contentID;
    
    [self.navigationController pushViewController:viewController animated:YES];
    
}

- (void)refreshData {
    
    self.dataIndex = 10;
    [self initListData];
    
}

- (void)getMoreData {
    
    self.dataIndex += 10;
    [self initListData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
